import { Component, OnInit, Inject, OnDestroy } from "@angular/core";

import { Store, select } from "@ngrx/store";
import { Actions } from "@ngrx/effects";
import * as fromUser from "@/user/store/reducers/index";
import * as fromRoot from "@/reducers/index";
import { UserActions } from "@/user/store/actions";
import { ILoginRequest } from "@/user/store/models/user";
import { filter } from "rxjs/operators";

@Component({
  selector: "app-hack",
  templateUrl: "./hack.component.html",
  styleUrls: ["./hack.component.scss"]
})
export class HackComponent implements OnInit, OnDestroy {
  lists = [
    {
      coin: "ETH"
    },
    {
      coin: "LSK"
    },
    {
      coin: "BTC"
    }
  ];

  sub$ = {
    userSub$: null
  };

  uid = "";

  user$ = this.store.pipe(
    select(fromRoot.user),
    filter(user => !!user)
  );

  constructor(
    @Inject("AssetsService") private assetsService,
    @Inject("UtilsService") private utilsService,
    private store: Store<fromUser.State | fromRoot.State>
  ) {}

  ngOnInit() {
    this.sub$.userSub$ = this.user$.subscribe(e => {
      // this.uid = e.id;
    });
  }

  onCharge(item) {
    this.assetsService
      .charge({
        coin: item.coin,
        amount: 100000,
        uid: this.uid
      })
      .subscribe(e => {
        if (200 === e.code) {
          alert("success");
        }
      });
  }

  ngOnDestroy() {
    this.utilsService.unsub(this.sub$);
  }
}
